package com.sunducation.wts.databinding5

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView

class MyAdapter : RecyclerView.Adapter<MyAdapter.Companion.ViewHolder>{
    private var mData:ArrayList<Student>  = ArrayList<Student>()
    constructor(data: ArrayList<Student>) {
        mData.addAll(data)
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding:ViewDataBinding  = DataBindingUtil.inflate(
            LayoutInflater
            .from(parent.getContext()), R.layout.item, parent, false)
        val holder:ViewHolder  = ViewHolder(binding.getRoot())
        holder.binding = binding
        return holder;
    }

    override fun getItemCount(): Int {
        return mData.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.binding.setVariable(BR.stu, mData.get(position))
        holder.binding.executePendingBindings() //立即刷新UI
    }

    companion object{
        class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

            lateinit var binding: ViewDataBinding
        }
    }


}